package com.itheima.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Menus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author zhushaoie
 * @Date 2021/6/29 9:53
 * @Version 1.0
 */
@Service(interfaceClass =MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
//        页码
        Integer currentPage = queryPageBean.getCurrentPage();
//        每页记录数
        Integer pageSize = queryPageBean.getPageSize();
//      查询条件
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
//        查询到的数据是Menu
        Page<Menu> page = menuDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Menu> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public List<Menu> findChildrenMenu(Integer parentMenuId) {
        return menuDao.findChildrenMenu(parentMenuId);

    }

    @Override
    public List<Map<String,Integer>> findByName() {

        return menuDao.findByName();
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void delete(Integer id) {
        menuDao.delete(id);
    }

    @Override
    public List<Menus> findMenuByUsername(String username) {
        List<Menus> list=new ArrayList<>();
        List<Menu> menuByUsername02 = menuDao.findMenuByUsername02(username);
        for (Menu menu02: menuByUsername02) {
            Menus menuF = new Menus();
            menuF.setTitle(menu02.getName());
            menuF.setPath(menu02.getPath());
            menuF.setIcon(menu02.getIcon());
            Integer id = menu02.getId();
            List<Menus> menus = menuDao.findMenuByUsername(id);
            menuF.setChildren(menus);
            list.add(menuF);
        }
        return list;
    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }
}
